package com.app.unionpay;

import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;

import com.jiangdg.usbcamera.utils.Lf_Debug;
import com.lf.emv.api;
import com.lf.emv.jni;
import com.out.outemv.Emv_Debug;
import com.out.outpara.CommConfig;
import com.out.outpara.OuterAppConfig;
import com.out.outpara.ParamFactory;
import com.out.outsys.defaultparam;
import com.out.transactioncallback.TransactionCallBack;

import java.util.Iterator;
import java.util.Map;



public abstract class unionpay {

	private Application application;
	private ParamFactory ParamProcy;

	
    public Application getApplication(){
    	
    	return this.application;
    }

	public void init(Application application)
    {
		this.application = application;
        new Executer<Boolean>()
        {
            public Boolean onExecute()
            {         
            	jni.getInstance();
                Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(), "----------app param init------------");
                ParamProcy.getParameterManager().init(getApplication());   
                if(firstRun()){
                    onFirstRun();
                }
                
                api myapi;
            	Emv_Debug mydebug[]= new Emv_Debug[1];
            	int debugflag = Lf_Debug.dbg_getcard();
            	if(debugflag > 0) {
					myapi = api.getInstance();//new api();
					mydebug[0] = new Emv_Debug();
					myapi.Emv_DebugSet(mydebug);
					myapi.Emv_DebugCtrl(debugflag);
				}
                return true;
            }
        }.execute();
		
		
    }
	
	public void setConfigParam(final String key, final String value, final TransactionCallBack transactioncallback){
   	 new Executer<Boolean>()
        {
   		 public Boolean onExecute()
            {   
		        if(TextUtils.isEmpty(key))
		        {
		            Bundle bundle = new Bundle();
		            bundle.putString("errorCode", "FI");
		            transactioncallback.onError(defaultparam.SYS_normalerr, bundle);
		            return Boolean.valueOf(true);
		        }
		        ParamProcy.getParameterManager().setStringValue(key, value);
		        ParamProcy.getParameterManager().syncValue();
		       
		        transactioncallback.onSuccess("参数保存成功", null);
		        return Boolean.valueOf(true); 
            }
        }.execute();
   }
	
	public void setConfigParams(final Map<String, String>params, final TransactionCallBack transactioncallback){
   	 new Executer<Boolean>()
        {
            public Boolean onExecute()
            {
		        if(params == null)
		        {
		            Bundle bundle = new Bundle();
		            bundle.putString("errorCode", "FI");
		            transactioncallback.onError(defaultparam.SYS_normalerr, bundle);
		            return Boolean.valueOf(true);
		        }
		        
		        Iterator <Map.Entry<String,String>> entries = params.entrySet().iterator();
		        while(entries.hasNext()){
		            Map.Entry<String,String> entry = (Map.Entry<String, String>)entries.next();
		            if(TextUtils.isEmpty((CharSequence)entry.getKey()))
		            {
		                Bundle bundle = new Bundle();
		                bundle.putString("errorCode", "FI");
		                transactioncallback.onError(defaultparam.SYS_normalerr, bundle);
		                return Boolean.valueOf(true);
		            }
		            ParamProcy.getParameterManager().setStringValue((String)entry.getKey(), (String)entry.getValue());
		        }
		        ParamProcy.getParameterManager().syncValue();
		        if((params.containsKey(CommConfig.CHECK_HOST_URL))
		        	&& ("1".equals(params.get(CommConfig.HOST_URL))))
		        {
			        {
			        	transactioncallback.onSuccess("参数保存成功", null);
			        }
		        }		        
	        	else 
		        {
		        	transactioncallback.onSuccess("参数保存成功", null);
		        }
		        
		        return Boolean.valueOf(true);
            }
        }.execute();
   }
   
   public String getConfigParam(final String key) {
   	 return new Executer<String>()
        {
   		 public String onExecute()
            {   
           	 return ParamProcy.getParameterManager().getStringValue(key, "");
            }
        }.execute();
   }
   
	
    private boolean firstRun(){
    	 return new Executer<Boolean>()
         {
             public Boolean onExecute()
             {
            	 return "true".equals(ParamProcy.getParameterManager().getStringValue("firstRun", "true"));
             }
         }.execute();
    }
    
    
    private void onFirstRun(){
    	 new Executer<Boolean>()
         {
             public Boolean onExecute()
             {
		        ParamProcy.getParameterManager().setStringValue(OuterAppConfig.TRACE_NO, defaultparam.DEFAULT_TRANCENO);
		        ParamProcy.getParameterManager().setStringValue(OuterAppConfig.BATCH_NO, defaultparam.DEFAULT_BATCHNO);
		        ParamProcy.getParameterManager().setStringValue("firstRun", "false");
		        ParamProcy.getParameterManager().syncValue();
		        return Boolean.valueOf(true);
             }
         }.execute();
    }
    
    protected abstract ParamFactory getParamFactory();
    protected abstract int getParamId();

    public abstract class Executer<T>
    {
    	  public T execute() 
    	  { 
    		  synchronized (this) 
    		  {   			  
    			  //ParamProcy.setParamFactory(unionpay.this.getParamFactory());  
    			  ParamProcy = getParamFactory();    			  
    			  return onExecute();
    	      }
    	  }    	  
    	  public abstract T onExecute();
    }
}
